package com.javafactroy.kunlong.base.metadata.valueobj;

import cn.hutool.core.util.StrUtil;
import com.javafactroy.kunlong.codefactroy.convert.EntityPropConvert;
import com.javafactroy.kunlong.codefactroy.service.domain.valueobj.EntityProp;
import com.javafactroy.kunlong.codefactroy.service.domain.valueobj.EntityProps;
import com.javafactroy.kunlong.codefactroy.service.stringtemplate.StringTemplate;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Data
@Accessors(chain = true)
public class MetadataTable implements Serializable {

    private String dbName;
    private String tableName;
    private String tableComment;
    private String clsName;

    private List<MetadataField> fieldList;

    private EntityProps entityProps;

    public MetadataTable() {
        fieldList = new ArrayList<>();
    }

    String col2PropType() {
        return "";
    }

    public EntityProps toEntityProps() {
        entityProps = new EntityProps();

        entityProps.setDbName(dbName);
        entityProps.setTableName(tableName);
        entityProps.setTableComment(tableComment);
        entityProps.setStringTemplate(new StringTemplate());
        entityProps.setClsName(StrUtil.upperFirst(StrUtil.toCamelCase(tableName)));

        entityProps.setPropList(EntityPropConvert.INSTANCE.toList(getFieldList()));

        EntityProp prop = entityProps.findPkey();
        entityProps.setPkey(prop == null ? "pkey" : prop.getColName());

        return entityProps;
    }
}
